from PySide import QtGui
import g_view
import b_field

class EditFields(QtGui.QWidget):
	
	def __init__(self, parent):
		super(EditFields, self).__init__()
		self.parent = parent
		self.errormsg = QtGui.QErrorMessage()
		self.initUI()

	def initUI(self):
		self.listfields = QtGui.QListWidget()
		self.listfields.addItems(self.parent.data.fields.keys())
		self.listfields.sortItems()
		self.listfields.itemSelectionChanged.connect(self.selectionChanged)
		
		row = 0
		grid = QtGui.QGridLayout()
		name = QtGui.QLabel('Name')
		self.nameedit = QtGui.QLineEdit()
		grid.addWidget(name, row, 0)
		grid.addWidget(self.nameedit, row, 1)
		row += 1
		vector = QtGui.QLabel('Vector')
		grid.addWidget(vector, row, 0)
		self.vectoredit = []
		for i in range(0, self.parent.data.settings.dimensions):
			self.vectoredit.append(QtGui.QLineEdit())
			self.vectoredit[i].setValidator(QtGui.QDoubleValidator())
			grid.addWidget(self.vectoredit[i], row, 1)
			row += 1
		
		self.addButton = QtGui.QPushButton("Add Field")
		self.addButton.clicked.connect(self.addField)
		self.modifyButton = QtGui.QPushButton("Modify Field")
		self.modifyButton.clicked.connect(self.modifyField)
		self.deleteButton = QtGui.QPushButton("Delete Field")
		self.deleteButton.clicked.connect(self.deleteField)
		buttons = QtGui.QHBoxLayout()
		buttons.addWidget(self.addButton)
		buttons.addWidget(self.modifyButton)
		buttons.addWidget(self.deleteButton)
		
		editdialog = QtGui.QVBoxLayout()
		editdialog.addLayout(grid)
		editdialog.addStretch(1)
		editdialog.addLayout(buttons)
		
		hbox = QtGui.QHBoxLayout()
		hbox.addWidget(self.listfields)
		hbox.addLayout(editdialog)
		
		self.setLayout(hbox)
		self.setWindowTitle("Edit Fields")
		
	def closeEvent(self, event):
		self.hide()
	
	def modifyField(self):
		current = self.listfields.currentItem()
		if current is not None and current.isSelected():
			name = str(current.text())
			for i in range(0, self.parent.data.settings.dimensions):
				self.parent.data.fields[name].g[i] = float(self.vectoredit[i].text())
			nameedit = str(self.nameedit.text())
			if nameedit != name:
				if nameedit in self.parent.data.fields.keys():
					self.errormsg.showMessage("Field by that name already exists")
					return
				self.parent.data.fields[name].name = nameedit
				self.parent.data.fields[nameedit] = self.parent.data.fields[name]
				del self.parent.data.fields[name]
				self.parent.updateAll()
			else:
				self.parent.updateAll(self)
		else:
			self.errormsg.showMessage("No field selected to modify")
	
	def addField(self):
		g = []
		for i in range(0, self.parent.data.settings.dimensions):
			g.append(float(self.vectoredit[i].text()))
		tempfield = b_field.Field(self.parent.data, self.nameedit.text(), g)
		if tempfield.name in self.parent.data.fields:
			self.errormsg.showMessage("Field by that name already exists, cannot add")
		else:
			self.parent.data.fields[tempfield.name] = tempfield
			self.parent.updateAll()
	
	def deleteField(self):
		current = self.listfields.currentItem()
		if current is not None and current.isSelected():
			name = str(current.text())
			self.parent.data.fields[name].deleteField()
			self.parent.updateAll()
		else:
			self.errormsg.showMessage("No field selected to delete")
	
	def selectionChanged(self):
		current = self.listfields.currentItem()
		if current is not None and current.isSelected():
			name = current.text()
			self.nameedit.setText(name)
			for i in range(0, self.parent.data.settings.dimensions):
				self.vectoredit[i].setText(str(self.parent.data.fields[name].g[i]))
	
	def updateFields(self, check=None):
		if not check is self:
			if self.listfields.count() != len(self.parent.data.members) or check == "ALL":
				self.listfields.itemSelectionChanged.disconnect(self.selectionChanged)
				self.listfields.clear()
				self.listfields.addItems(self.parent.data.fields.keys())
				self.listfields.sortItems()
				self.listfields.itemSelectionChanged.connect(self.selectionChanged)
